using Services;
using Skills;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class SkillUI_Button : MyButton, IPointerEnterHandler, IPointerExitHandler
{
    private Services.EventSystem eventSystem;
    private SkillUI_Manager manager;
    private RuntimeSkill m_skill;
    private float offset;

    private Image m_image;

    protected override void Awake()
    {
        base.Awake();
        manager = GetComponentInParent<SkillUI_Manager>();
        offset = manager.Panel.Width / 2;
        m_image = GetComponentsInChildren<Image>()[1];
        eventSystem = ServiceLocator.Get<Services.EventSystem>();
    }

    public void Initialize(RuntimeSkill skill)
    {
        m_skill = skill;
        m_text.text = m_skill.Name;
        m_image.sprite = m_skill.SkillAttached.icon;
        m_button.interactable = skill.CanCast();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        string information = m_skill.GetInformation();
        eventSystem.Invoke<string, Vector2, float>(EEvent.View, information, transform.position, offset);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        eventSystem.Invoke(EEvent.EndView);
    }

    protected override void OnClick()
    {
        eventSystem.Invoke(EEvent.PlayerSelectSkill, m_skill);
    }
}
